package com.ifar.audiosdk.api

import com.franmontiel.persistentcookiejar.PersistentCookieJar
import com.franmontiel.persistentcookiejar.cache.SetCookieCache
import com.franmontiel.persistentcookiejar.persistence.SharedPrefsCookiePersistor
import com.ifar.audiosdk.IfarBaseApp
import okhttp3.OkHttpClient

object IfarRetrofitClient : IfarBaseRetrofitClient() {

    val service by lazy { IfarRetrofitClient.getService(IfarBaseApi::class.java, IfarBaseApi.BASE_URL) }

    private val cookieJar by lazy {
        PersistentCookieJar(
            SetCookieCache(),
            SharedPrefsCookiePersistor(IfarBaseApp.CONTEXT)
        )
    }

    override fun handleBuilder(builder: OkHttpClient.Builder) {
        builder.cookieJar(cookieJar)
            .addInterceptor { chain ->
                val request = chain.request()
                    .newBuilder()
                    .build()
                chain.proceed(request)
            }

    }
}